package org.example.socket;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

/**
 * @BelongsProject: javaFoundation
 * @BelongsPackage: org.example.socket
 * @Author: dtq
 * @CreateTime: 2023-11-01  16:56
 * @Description: TODO
 * @Version: 1.0
 */
public class StreamUtils {

    public static byte[] streamToByteArray(InputStream is) throws Exception {
        //创建输出流对象
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        //字节数组
        byte[] b = new byte[1024];
        int len;
        //循环读取
        while ((len = is.read(b)) != -1) {
            //把读取到的数据，写入bos
            bos.write(b, 0, len);
        }
        //然后将bos 转成字节数组bos.close() ;
        byte[] array = bos.toByteArray();
        return array;


    }
}
